from django.db import models


class DailyStatistics(models.Model):
    user_id = models.BigIntegerField(verbose_name = "用户ID")
    date = models.DateField(verbose_name = "统计日期", unique = True)
    focus_minutes = models.IntegerField(default = 0, verbose_name = "专注时长")
    focus_sessions = models.IntegerField(default = 0, verbose_name = "专注次数")
    p_streak_days = models.IntegerField(default = 0, verbose_name = "打卡连续天数")
    streak_days = models.IntegerField(default = 0, verbose_name = "专注连续天数")
    created_at = models.DateTimeField(auto_now_add = True, verbose_name = "创建时间")
    updated_at = models.DateTimeField(auto_now = True, verbose_name = "更新时间")

    class Meta:
        verbose_name = "单日统计数据"
        verbose_name_plural = "单日统计数据"

    def __str__(self):
        return f"Daily statistics for user {self.user_id} on {self.date}"


class StatisticCache(models.Model):
    RANGE_TYPE_CHOICES = [
        ('day', 'Day'),
        ('last7', 'Last 7 Days'),
        ('last30', 'Last 30 Days'),
        ('last90', 'Last 90 Days'),
        ('custom', 'Custom'),
    ]

    user_id = models.BigIntegerField(verbose_name = "用户ID")
    range_type = models.CharField(max_length = 20, choices = RANGE_TYPE_CHOICES, verbose_name = "范围类型")
    start_date = models.DateField(verbose_name = "起始日期")
    end_date = models.DateField(verbose_name = "截止日期")
    total_minutes = models.IntegerField(default = 0, verbose_name = "总分钟数")
    avg_minutes = models.FloatField(default = 0, verbose_name = "平均分钟数")
    cal_streak_days = models.IntegerField(default = 0, verbose_name = "累计天数")
    task_distribution = models.JSONField(default = dict, verbose_name = "任务分布")
    hour_distribution = models.JSONField(default = dict, verbose_name = "时段分布")
    time_trend = models.JSONField(default = dict, verbose_name = "时长趋势")
    cache_version = models.IntegerField(default = 1, verbose_name = "缓存版本")
    last_updated = models.DateTimeField(auto_now = True, verbose_name = "更新时间")

    class Meta:
        verbose_name = "范统计围数据"
        verbose_name_plural = "范统计围数据"

    def __str__(self):
        return f"Statistic cache for user {self.user_id} from {self.start_date} to {self.end_date}"


class StatisticQueries(models.Model):
    user_id = models.BigIntegerField(verbose_name = "用户ID")
    range_type = models.CharField(max_length = 20, verbose_name = "查询类型")
    query_count = models.IntegerField(default = 1, verbose_name = "查询次数")
    last_queried = models.DateTimeField(auto_now = True, verbose_name = "更新时间")

    class Meta:
        verbose_name = "自主查询记录"
        verbose_name_plural = "自主查询记录"

    def __str__(self):
        return f"Queries for user {self.user_id} - {self.range_type}"
